﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HA.Data
{
    /// <summary>
    /// 存储辅助绘图相关的文件名称，以及设置
    /// </summary>
    [Serializable]
    public class AssistData:ICloneable
    {
        public AssistData()
        {

            
        }

        [field:NonSerialized]
        public event EventHandler<BitImageCollectionChangedEventArgs> BitImageChanged;


        string _bitImageId = "bitImage";

        public string BitImageId
        {
            get { return _bitImageId; }
            set { _bitImageId = value; }
        }

        int _bitImageIdIndex;

        //public int BitImageIdIndex
        //{
        //    get { return _bitImageIdIndex; }
        //    set { _bitImageIdIndex = value; }
        //}

        Dictionary<string, BitImageInfoes> _bitImageIdAndInfoes = new Dictionary<string, BitImageInfoes>();
        /// <summary>
        /// key表示id及存储名称，value包含图像信息
        /// </summary>
        public Dictionary<string, BitImageInfoes> BitImageIdAndInfoes
        {
            get { return _bitImageIdAndInfoes; }
        }

        #region 公共方法


        public string GetNextBitImageId()
        {
            int index = this._bitImageIdIndex;

            this._bitImageIdIndex += 1;

            return "bitImage" + index.ToString();

        }



        /// <summary>
        /// 添加位图
        /// </summary>
        /// <param name="filePaths">图像路径， 及文件名（不包含路径）做Id</param>
        /// <param name="infoes">图像位置大小名称等信息</param>
        public void AddBitImage(Dictionary<string, BitImageInfoes> idAndInfoes)
        {
            foreach (string item in idAndInfoes.Keys)
            {
                this._bitImageIdAndInfoes.Add(item, idAndInfoes[item]);
            }

            this.OnAddBitImage(idAndInfoes);
        }

        /// <summary>
        /// 删除位图
        /// </summary>
        /// <param name="id"></param>
        public void RemoveBitImage(string id)
        {
            this._bitImageIdAndInfoes.Remove(id);
            string idWithNoExt = System.IO.Path.GetFileNameWithoutExtension(id);

            List<string> ids = new List<string>();
            ids.Add(idWithNoExt);

            this.OnRemoveBitImage(ids);
        }

        /// <summary>
        /// 删除位图
        /// </summary>
        /// <param name="ids"></param>
        public void RemoveBitImage(List<string> ids)
        {
            if (ids == null) return;

            List<string> idsWithNoExt = new List<string>();

            for (int i = 0; i < ids.Count; i++)
            {
                this._bitImageIdAndInfoes.Remove(ids[i]);
                idsWithNoExt.Add(System.IO.Path.GetFileNameWithoutExtension(ids[i]));
            }

            this.OnRemoveBitImage(idsWithNoExt);
        }

        #endregion

        #region 内部方法


        void OnAddBitImage(Dictionary<string, BitImageInfoes> idAndInfoes)
        {
            if (this.BitImageChanged != null)
            {
                this.BitImageChanged(this,new BitImageCollectionChangedEventArgs(idAndInfoes));
            }
        }

        void OnRemoveBitImage(List<string> ids)
        {
            if (this.BitImageChanged != null)
            {
                this.BitImageChanged(this, new BitImageCollectionChangedEventArgs(ids));
            }
        }

        #endregion

        #region Clone

        public object Clone()
        {
            AssistData ad = (AssistData)this.MemberwiseClone();

            ad._bitImageIdAndInfoes = new Dictionary<string,BitImageInfoes>();
            foreach (string item in this._bitImageIdAndInfoes.Keys)
            {
                ad._bitImageIdAndInfoes.Add(item, (BitImageInfoes)this._bitImageIdAndInfoes[item].Clone());
            }

            return ad;
        }

        #endregion
    }
}
